<?php

namespace App\Console\Commands;

use App\Jobs\CancelWarning;
use App\Jobs\CheckWarning;
use App\Model\Device;
use App\Model\DeviceOffLog;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;

class DeviceCancelWarningCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'device:cancel_warning';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $today = date('Y-m-d');
        $list = Device::query()
            ->where('net_type',1)
            ->where('house_id', '>', 0)
            ->where('is_warning',1)
            ->where(function ($query) use ($today) {
                $query->where('cancel_data', '<', $today)
                    ->orWhereNull('cancel_data'); // 兼容 NULL 值
            })
            ->limit(300)
            ->get(['id']);
        foreach ($list as $v) {
            dispatch(new CancelWarning($v->id));
        }
    }
}
